<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Tag extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('tag_model');

		$this->template->set_breadcrumb('Tag', 'backend/tag');
	}

	public function index()
	{
		//$this->auth->check_uri_permissions(true, 'backend/login');
		$this->template->title('SiteName', 'Tag');
		$this->template->build('backend/tag/tag', $this->data);
	}

	public function load($order_by = 'id', $order_dir = 'desc', $page = 1)
	{
		$pager_config['base_url'] = base_path() . "backend/tag/load/{$order_by}/{$order_dir}/";
		$pager_config['uri_segment'] = 6;
		$pager_config['num_links'] = 3;
		$pager_config['total_rows'] = $this->tag_model->count();
		$pager_config['per_page'] = 20;

		$limit = $pager_config['per_page'];
		$offset = $page ? (($page - 1) * $limit) : 0;
		$tags = $this->tag_model->find(NULL, "{$order_by} {$order_dir}", $limit, $offset)->result();
		$data['sql'] = $this->db->last_query();

		$data['rows'] = "";
		foreach ($tags as $row)
		{
			$data['rows'].= "<tr><td><input type=\"checkbox\" name=\"items[]\" value=\"{$row->id}\" /></td>";
			$data['rows'].= "	<td>" . $row->name . "</td>";
			$data['rows'].= "	<td align=\"center\"><div><a href=\"". base_path() . "backend/tag/update/{$row->id}\" class=\"edit\">Edit</a></div></td></tr>";
		}

		$data['from_record'] = $pager_config['total_rows'] == 0 ? 0 : $offset + 1;
		$data['to_record'] = $offset + $limit > $pager_config['total_rows'] ? $pager_config['total_rows'] : $offset + $limit;
		$data['total_record'] = $pager_config['total_rows'];

		$this->pagination->initialize($pager_config);
		$data['pagination'] = $this->pagination->create_links();
		$data['message'] = 'OK';

		echo json_encode($data);
	}

	public function create()
	{
		$this->data['message'] = '';
		$validator = $this->form_validation;
		$validator->set_rules('name', 'Tên Tag', 'trim|required|xss_clean|min_length[3]|max_length[32]');
		if($validator->run())
		{
			$data['name'] = $validator->set_value('name');
			$data['created'] = date('Y-m-d H:i:s');

			$this->tag_model->insert_ignore($data);
			$this->data['message'] = 'Dữ liệu thêm vào thành công';
		}
		else
		{
			$this->data['message'] = strip_tags(validation_errors());
		}

		$this->template->set_breadcrumb('Tạo mới', 'backend/tag/create');
		$this->template->title('SeoShop', 'Tag', 'Tạo mới');
		$this->template->build('backend/tag/create', $this->data);
	}

	public function delete($tag_id = NULL)
	{
		if($this->input->is_ajax_request())
		{
			if($tag_ids = $this->input->post('items'))
			{
				foreach ((array)$tag_ids as $tag_id)
				{
					$this->tag_model->delete(array('id' => $tag_id));
					$this->product_tag_model->delete(array('tag_id' => $tag_id));
				}
			}

			$data['message'] = 'OK';
			die(json_encode($data));
		}
	}

	public function update($tag_id = NULL)
	{
		if($tag_id)
		{
			$this->data['message'] = '';

			$validator = $this->form_validation;
			$validator->set_rules('name', 'Tên tag', 'trim|required|xss_clean|min_length[3]|max_length[32]');

			if($validator->run())
			{
				$data['name'] = $validator->set_value('name');

				if($this->tag_model->count(array('id <> ' => $tag_id, 'name' => $data['name'])))
				{
					$this->data['message'] = 'Tag này đã tồn tại. Hãy chọn cái khác.';
				}
				else
				{
					$this->tag_model->update(array('id' => $tag_id), $data);
					$this->data['message'] = 'Chỉnh sửa thành công.';
				}
			}
			else
			{
				$this->data['message'] = validation_errors('<span>', '</span>');
			}

			$this->data['tag'] = $this->tag_model->find(array('id' => $tag_id))->row();
			$this->template->title('SiteName', 'Tag', 'Chỉnh sửa');
			$this->template->build('backend/tag/update', $this->data);
		}
		else
		{
			show_404();
		}
	}
}